		TITLE	OPENREAD - Copyright (c) 1994 SLR Systems

		INCLUDE	MACROS
		INCLUDE	IO_STRUC

;		PUBLIC	OPEN_READING


		.DATA

		EXTERNDEF	MODULE_NAME:BYTE

		EXTERNDEF	OBJ_DEVICE:DWORD,CURN_FILE_LIST_GINDEX:DWORD

		EXTERNDEF	STUBPATH_LIST:FILE_LISTS,OBJPATH_LIST:FILE_LISTS,LIBPATH_LIST:FILE_LISTS,_FILE_LIST_GARRAY:FILE_LISTS
		EXTERNDEF	MYI_STUFF:MYI_STRUCT


		.CODE	ROOT_TEXT

		EXTERNDEF	_capture_eax:proc
		EXTERNDEF	_move_nfn:proc,ERR_NFN_ABORT:PROC,WARN_NFN_RET:PROC,_fancy_open_input:proc,CAPTURE_EAX:PROC
		EXTERNDEF	_read_16k_threaded:proc,_delete_phantom_path:proc

		EXTERNDEF	FILE_NOT_FOUND_ERR:ABS


OR_VARS		STRUC

OR_FILNAM_BP	NFN_STRUCT<>

OR_VARS		ENDS


FIX		MACRO	X

X		EQU	([EBP].OR_VARS.(X&_BP))

		ENDM


FIX	OR_FILNAM


OPEN_READING	PROC

if	fgh_inthreads
		BITT	_HOST_THREADED
		JZ	OPEN_READING_1

		;
		;CALC CURRENT THREAD - ALWAYS VALID FOR OBJ'S
		;
		;EAX IS FILE_LIST_GINDEX
		;
		MOV	CURN_FILE_LIST_GINDEX,EAX

		PUSHM	EDI,ESI

		PUSH	EBX
		CONVERT	EAX,EAX,_FILE_LIST_GARRAY
		ASSUME	EAX:PTR FILE_LIST_STRUCT
		MOV	EAX,[EAX].FILE_LIST_THREAD	;ASSIGNED TO THIS THREAD

		IMUL	EBX,EAX,SIZE MYI_STRUCT

		LEA	EAX,MYI_STUFF[EBX]
		MOV	EBX,MYI_STUFF[EBX].MYI_LOCALS
		ASSUME	EBX:PTR MYI2_STRUCT

		PUSH	EAX
		;
		;GET MYNAMS STRUCTURE PLEASE
		;
		MOV	EAX,[EBX].MYI2_NEXT_OPEN_FILE
		ASSUME	EAX:NOTHING

		INC	EAX

		AND	EAX,3

		IMUL	ESI,EAX,SIZE NFN_STRUCT

		IMUL	EDI,EAX,SIZE OPEN_STRUCT

		LEA	ESI,[EBX+ESI].MYI2_NAMS
		MOV	[EBX].MYI2_NEXT_OPEN_FILE,EAX
		ASSUME	ESI:PTR NFN_STRUCT

		LEA	EDI,[EBX+EDI].MYI2_OPEN_STRUC
		ASSUME	EDI:PTR OPEN_STRUCT

		MOV	[EBX].MYI2_LAST_OPENFILE,EDI

		LEA	EAX,[EBX].OPENFILE_OPEN_SEM 	;MAKE SURE FILE OPEN BEFORE TOUCHING ANYTHING IN
		CALL	CAPTURE_EAX			;OPENFILE OR NFN_STRUC
		;
		;OK, ITS OPEN, AND I OWN IT
		;

		MOV	EAX,[EDI].OPENFILE_FLAGS
		MOV	ECX,[EDI].OPENFILE_PATH_GINDEX

		PUSH	EAX
		MOV	EDI,CURN_FILE_LIST_GINDEX

		CONVERT	EDI,EDI,_FILE_LIST_GARRAY
		ASSUME	EDI:PTR FILE_LIST_STRUCT

		TEST	ECX,ECX
		JZ	L1$

		MOV	[EDI].FILE_LIST_PATH_GINDEX,ECX
L1$:
		MOV	AL,[EDI].FILE_LIST_NFN.NFN_FLAGS

		TEST	AL,MASK NFN_TIME_VALID
		JNZ	L2$

		OR	AL,MASK NFN_TIME_VALID
		MOV	ECX,DPTR [ESI].NFN_TIME

		MOV	[EDI].FILE_LIST_NFN.NFN_FLAGS,AL
		MOV	EAX,[ESI].NFN_FILE_LENGTH

		MOV	DPTR [EDI].FILE_LIST_NFN.NFN_TIME,ECX
		MOV	[EDI].FILE_LIST_NFN.NFN_FILE_LENGTH,EAX
L2$:
		MOV	EAX,[ESI].NFN_PATHLEN
		MOV	EDX,ESI

		MOV	EDI,OFF MODULE_NAME+4	;SET MODULE_NAME TO FILENAME
		MOV	ECX,[ESI].NFN_PRIMLEN
		ASSUME	EDI:NOTHING

		LEA	ESI,[ESI+EAX].NFN_TEXT
		MOV	[EDI-4],ECX

		REP	MOVSB

		POPM	ECX,EBX
		ASSUME	EBX:PTR MYI_STRUCT

		XOR	EAX,EAX

		MOV	[EBX].MYI_BYTE_OFFSET,EAX
		MOV	[EBX].MYI_PHYS_ADDR,EAX

		MOV	[EBX].MYI_COUNT,EAX	;BUFFER EMPTY
		MOV	[EBX].MYI_BUFCNT,AL

		MOV	[EBX].MYI_FILLBUF,OFF _read_16k_threaded

		MOV	EAX,CURN_FILE_LIST_GINDEX
		MOV	OBJ_DEVICE,EBX

		MOV	[EBX].MYI_FILE_LIST_GINDEX,EAX
		MOV	EAX,EBX

		POP	EBX

		POPM	ESI,EDI

		TEST	ECX,ECX
		JZ	FNF

		RET

FNF:
		PUSH	ESI
		MOV	ESI,EDX

		push	ESI
		call	_delete_phantom_path
		add	ESP,4

		MOV	EDX,ESI
		POP	ESI

		MOV	ECX,EDX
		MOV	AL,FILE_NOT_FOUND_ERR
if	fg_segm
		MOV	DL,[EDX].NFN_STRUCT.NFN_TYPE

		CMP	DL,NFN_OLD_TTYPE
		JZ	FNF1

		CMP	DL,NFN_STUB_TTYPE
		JZ	FNF1

		CMP	DL,NFN_RES_TTYPE
		JZ	FNF1
endif
		CALL	ERR_NFN_ABORT
if	fg_segm
FNF1:
		CALL	WARN_NFN_RET

;		STC
		mov	EAX,0

		RET
endif

endif

OPEN_READING	ENDP


OPEN_READING_1	PROC
		;
		;EAX IS FILE_LIST_GINDEX
		;
		PUSH	EBP
		SUB	ESP,SIZE OR_VARS
		MOV	CURN_FILE_LIST_GINDEX,EAX
		MOV	EBP,ESP
		PUSHM	EDI,ESI,EBX
		;
		;DOS
		;
		CONVERT	EAX,EAX,_FILE_LIST_GARRAY

		LEA	EBX,OR_FILNAM
		ASSUME	EBX:PTR NFN_STRUCT

		LEA	ECX,[EAX].FILE_LIST_STRUCT.FILE_LIST_NFN
		MOV	EAX,EBX


		push	ECX
		push	EAX
		call	_move_nfn		;copy to that structure
		add	ESP,8

		LEA	ESI,[EBX].NFN_TEXT
		MOV	EDI,OFF MODULE_NAME+4	;SET MODULE_NAME TO FILENAME
		MOV	ECX,[EBX].NFN_PRIMLEN
		ADD	ESI,[EBX].NFN_PATHLEN
		MOV	[EDI-4],ECX

		REP	MOVSB
		MOV	[EDI],ECX

if	fg_segm OR fg_pe
		MOV	AL,[EBX].NFN_TYPE
		MOV	ECX,OFF STUBPATH_LIST
		CMP	AL,NFN_OLD_TTYPE		;OLD AND STUB USE PATH=
		JZ	L1$
		CMP	AL,NFN_STUB_TTYPE
		JZ	L1$
endif
		MOV	EAX,EBX
		MOV	ECX,OFF OBJPATH_LIST		;OTHERS USE OBJ= AND LIB=
		push	ECX
		push	EAX
		call	_fancy_open_input
		test	EAX,EAX
		jnz	L2$
		;JNC	L2$
		MOV	ECX,OFF LIBPATH_LIST
L1$:
		MOV	EAX,EBX
		push	ECX
		push	EAX
		call	_fancy_open_input	;OPEN THE FILE
		test	EAX,EAX
		jz	FNF
		;JC	FNF
L2$:
		MOV	OBJ_DEVICE,EAX
		MOV	ECX,CURN_FILE_LIST_GINDEX	;NOW UPDATE FLAGS, TIME, DATE, FILE-SIZE
		CONVERT	ECX,ECX,_FILE_LIST_GARRAY
		MOV	ESI,EBX
		LEA	EDI,[ECX].FILE_LIST_STRUCT.FILE_LIST_NFN
		MOV	ECX,NFN_STRUCT.NFN_PATHLEN/4
		REP	MOVSD
L9$:
		POPM	EBX,ESI,EDI
		LEA	ESP,[EBP+ SIZE OR_VARS]
		POP	EBP
		RET

FNF:
		MOV	AL,FILE_NOT_FOUND_ERR
		MOV	ECX,EBX
if	fg_segm
		MOV	DL,[EBX].NFN_TYPE
		CMP	DL,NFN_OLD_TTYPE		;OLD, STUB, RES WARNING
		JZ	FNF1
		CMP	DL,NFN_STUB_TTYPE
		JZ	FNF1
		CMP	DL,NFN_RES_TTYPE
		JZ	FNF2
endif
		CALL	ERR_NFN_ABORT	;OBJ ERROR
if	fg_segm

FNF2:
		BITT	RC_GUESSED		;DON'T WARN IF I JUST GUESSED AT A FILENAME ANYWAY...
		JNZ	FNF3
FNF1:
		CALL	WARN_NFN_RET
FNF3:
;		STC
		mov	EAX,0
		JMP	L9$

endif

OPEN_READING_1	ENDP


		END

